﻿using System;
using System.Windows.Forms;

namespace EasyIot.WinApp
{
    public partial class MainForm : Form
    {  
        public FormCase m_eCurFormCase = FormCase.Form_StandBy; 
        frmStandBy m_StandBy = null;
        frmLogin m_Login = null; 
        frmAdmin m_Admin = null; 

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            LoadChildForm(); 
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        private void LoadChildForm()
        {
            this.SuspendLayout(); 
            m_StandBy = new frmStandBy();
            m_Login = new frmLogin();
            m_Admin = new frmAdmin();
            AddForm(m_StandBy);
            AddForm(m_Login);
            AddForm(m_Admin);
            //m_StandBy.ShowForm();
            m_Admin.ShowForm();
        }

        /// <summary>
        /// 添加子窗体
        /// </summary>
        /// <param name="winForm"></param>
        private void AddForm(SystemForm winForm)
        {
            SetDoubleBuffered(winForm);
            winForm.eventShowForm += new EventHandler<BaseRedirectArgs>(this.OnShowForm);
            this.Controls.Add(winForm);
        }

        /// <summary>
        /// 开控件的双缓冲启,避免反射
        /// </summary>
        /// <param name="control"></param>
        protected void SetDoubleBuffered(Control control)
        {
            typeof(Control).InvokeMember("DoubleBuffered", System.Reflection.BindingFlags.SetProperty | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic, null, control, new object[] { true });
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }

        /// <summary>
        /// 显示指定窗体
        /// </summary>
        public void OnShowForm(object sender, BaseRedirectArgs e)
        {
            m_eCurFormCase = e.eFormCase;

            SuspendLayout();
            switch (e.eFormCase)
            {
                case FormCase.Form_StandBy:
                    m_StandBy.ShowForm(e.eRedirectType, e.oParam);
                    break;
                case FormCase.Form_Login:
                    m_Login.ShowForm(e.eRedirectType, e.oParam);
                    break; ;
                case FormCase.Form_Admin:
                    m_Admin.ShowForm(e.eRedirectType, e.oParam);
                    break;
            }
        }

        public FormCase GetCurFormCase()
        {
            return m_eCurFormCase;
        } 

        public void OnTTKeyBoardInput(KeyType eKeyType, String strInput)
        {
            switch (m_eCurFormCase)
            {
                case FormCase.Form_Login:
                    m_Login.OnTTKeyBoardInput(eKeyType, strInput);
                    break;
            }
        }

        /// <summary>
        /// 显示指定窗体
        /// </summary>
        public void OnMessage(string message)
        { 
            //switch (m_eCurFormCase)
            //{
            //    case FormCase.Form_Admin:
                    m_Admin.AddMessage(message);
            //        break;
            //}
        }
    }
}
